import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final TextEditingController keyController1 = new TextEditingController();
  final TextEditingController keyController2 = new TextEditingController();
  final TextEditingController keyController3 = new TextEditingController();
  final TextEditingController keyController4 = new TextEditingController();
  final TextEditingController keyController5 = new TextEditingController();
  final TextEditingController keyController6 = new TextEditingController();
  final TextEditingController keyController7 = new TextEditingController();
  final TextEditingController keyController8 = new TextEditingController();

  final TextEditingController resultController = new TextEditingController();

  var keyResult = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("关键字组合"),
      ),
      body: new Container(
        padding: EdgeInsets.all(10),
        child: new Column(
          children: [
            Text("提示：关键词以*分隔"),
            TextField(
              controller: keyController1,
            ),
            TextField(
              controller: keyController2,
            ),
            TextField(
              controller: keyController3,
            ),
            TextField(
              controller: keyController4,
            ),
            TextField(
              controller: keyController5,
            ),
            TextField(
              controller: keyController6,
            ),
            TextField(
              controller: keyController7,
            ),
            TextField(
              controller: keyController8,
            ),
            Container(
              padding: EdgeInsets.all(10),
              child: FlatButton(
                child: Text("生成",
                    style: new TextStyle(color: Colors.white, fontSize: 16)),
                color: Colors.teal,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(4)),
                onPressed: () {
                  keyResult = "";
                  resultController.text = "";
                  List<String> keyList1;
                  List<String> keyList2;
                  List<String> keyList3;
                  List<String> keyList4;
                  List<String> keyList5;
                  List<String> keyList6;
                  List<String> keyList7;
                  List<String> keyList8;

                  var keyStr1 = keyController1.text.toString();
                  if (keyStr1 != null) {
                    keyList1 = keyStr1.split("*");
                  }
                  var keyStr2 = keyController2.text.toString();
                  if (keyStr2 != null) {
                    keyList2 = keyStr2.split("*");
                  }
                  var keyStr3 = keyController3.text.toString();
                  if (keyStr3 != null) {
                    keyList3 = keyStr3.split("*");
                  }
                  var keyStr4 = keyController4.text.toString();
                  if (keyStr4 != null) {
                    keyList4 = keyStr4.split("*");
                  }
                  var keyStr5 = keyController5.text.toString();
                  if (keyStr5 != null) {
                    keyList5 = keyStr5.split("*");
                  }
                  var keyStr6 = keyController6.text.toString();
                  if (keyStr6 != null) {
                    keyList6 = keyStr6.split("*");
                  }
                  var keyStr7 = keyController7.text.toString();
                  if (keyStr7 != null) {
                    keyList7 = keyStr7.split("*");
                  }
                  var keyStr8 = keyController8.text.toString();
                  if (keyStr8 != null) {
                    keyList8 = keyStr8.split("*");
                  }

                  // for(int i1 = 0; i1 < keyList1.length; i1++) {
                  //   for(int i2 = 0; i2 < keyList2.length; i2++) {
                  //     for(int i3 = 0; i3 < keyList3.length; i3++) {
                  //       for(int i4 = 0; i4 < keyList4.length; i4++) {
                  //         for(int i5 = 0; i5 < keyList5.length; i5++) {
                  //           for(int i6 = 0; i6 < keyList6.length; i6++) {
                  //             keyResult += keyList1[i1] + " " + keyList2[i2] + " " + keyList3[i3] + " "
                  //                 + keyList4[i4] + " " + keyList5[i5] + " " + keyList6[i6] + "\n";
                  //             resultController.text = keyResult;
                  //           }
                  //         }
                  //       }
                  //     }
                  //   }
                  // }

                  for (int i8 = 0; i8 < keyList8.length; i8++) {
                    for (int i7 = 0; i7 < keyList7.length; i7++) {
                      for (int i6 = 0; i6 < keyList6.length; i6++) {
                        for (int i5 = 0; i5 < keyList5.length; i5++) {
                          for (int i4 = 0; i4 < keyList4.length; i4++) {
                            for (int i3 = 0; i3 < keyList3.length; i3++) {
                              for (int i2 = 0; i2 < keyList2.length; i2++) {
                                for (int i1 = 0; i1 < keyList1.length; i1++) {
                                  keyResult += keyList1[i1] +
                                      " " +
                                      keyList2[i2] +
                                      " " +
                                      keyList3[i3] +
                                      " " +
                                      keyList4[i4] +
                                      " " +
                                      keyList5[i5] +
                                      " " +
                                      keyList6[i6] +
                                      " " +
                                      keyList7[i7] +
                                      " " +
                                      keyList8[i8] +
                                      "\n";
                                  resultController.text = keyResult;
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }

                  setState(() {});
                },
              ),
            ),
            Column(
              children: [
                GestureDetector(
                  child: TextField(
                    maxLines: 9,
                    minLines: 9,
                    controller: resultController,
                  ),
                  onTap: () {
                    Clipboard.setData(ClipboardData(text: keyResult));

                    final snackbar = SnackBar(
                        content: Text("内容已复制到粘贴板"),
                        duration: Duration(seconds: 3));
                    Scaffold.of(context).showSnackBar(snackbar);
                  },
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
